package com.baizhi.zxt.service;

import com.baizhi.zxt.dao.UserMapper;
import com.baizhi.zxt.entity.User;
import com.baizhi.zxt.entity.UserExample;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userDao;

    @Override
    public Map<String, Object> modify(User user) {
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            User user1 = new User();
            user1.setId(user.getId());
            user1.setStatus(user.getStatus());
            UserExample userExample = new UserExample();
            userExample.createCriteria().andIdEqualTo(user.getId());
            map.put("message","修改成功！！！");
            map.put("status",200);
        }catch(Exception e){
            map.put("message","修改失败！！！");
            map.put("status",400);
        }
        return map;
    }

    @Override
    public Map<String, Object> queryPage(int page, int pageSize) {
        Map<String,Object> map = new HashMap<String,Object>();
        //计算分页时的起始行下标
        int offset = (page-1)*pageSize;
        //分页查询数据
        RowBounds rowBounds = new RowBounds(offset,pageSize);
        List<User> list = userDao.selectByRowBounds(null,rowBounds);
        map.put("rows",list);
        //查询总行数
        int total = userDao.selectCount(null);
        map.put("total",total);
        map.put("page",page);
        return map;
    }
}
